package com.Controls;

import com.HMI.Main;

public class pHSensor implements Runnable {

	private boolean Control = false; // Activity of the controller (on/off)
	private boolean Delay = false; // Delay between control loops
	private final double lowThresh = 7.3; // Lower bound on pH
	private final double hiThresh = 7.5; // Higher bound on pH
	private int intDelay = 0;
	//private final double dblPrec = 0.85; // Determine when to take off delay
	//private double dblPHB4 = 0; // pH prior to chemical injection
	//private double dblPHExp = 0; // expected pH injection

	private Main parent;
	
	public pHSensor(Main parent) {
		this.parent = parent;
	}
	
	@Override
	public void run() {
		while (true) {
			try {
				PHSensor();
			} catch (InterruptedException e) {	
		    }
			
			try {
				Thread.sleep(20000);
			} catch (InterruptedException e) {  }
		}
	}

	public void PHSensor() throws InterruptedException {
		// Functions for reading sensor and converting it to pH
		// dblTest is a place holder for the actual pH measurement
		
		//double dblPH = parent.getPhValue();
		double dblPH = 7.7;
		//System.out.println("PH: " + dblPH);
		outHMI(dblPH); // Output sensor readings to HMI

		// Determine when to activate controller
		if (dblPH > hiThresh) {
			Control = true;
		} else if (dblPH < lowThresh) {
			Control = false;
		}
		
		/*
		if (Delay)
			intDelay++;
		
		if (intDelay > 19) {
			intDelay = 0;
			Delay = false;
		}*/

		if (Control) {
			pHControl.Control();
			//Delay = true;
		}

		/*
		// Determine when to take off delay
		if ((dblPHB4 - dblPH) > (dblPrec * dblPHExp)) {
			Delay = false;
		}

		if (Control && !Delay) {
			dblPHB4 = dblPH;
			dblPHExp = Controller.Control(dblPH);
			Delay = true;
		}
		*/
	}

	private void outHMI(double dblPpm) {
		// Pass pH value to HMI for graphing stuff
	}
}
